<% locals.cssSection.push('/lib/bootstrap/css/bootstrap.min.css') %>
<% locals.cssSection.push('/lib/markitup/skins/markitup/style.css') %>
<% locals.cssSection.push('/lib/markitup/sets/wiki/style.css') %>
<% locals.cssSection.push('/stylesheets/global.css') %>
<% locals.cssSection.push('/stylesheets/usermanage/global.css') %>
<% locals.cssSection.push('/stylesheets/usermanage/edit.css') %>

<% locals.cssSection.push('/lib/jquery-ui-1.9.2/css/blitzer/jquery-ui-1.9.2.custom.min.css') %>

<% locals.scriptSection.push('/lib/jquery-ui-1.9.2/js/jquery-ui-1.9.2.custom.min.js') %>

<% locals.scriptSection.push('/lib/markitup/jquery.markitup.js') %>
<% locals.scriptSection.push('/lib/markitup/sets/wiki/set.js') %>
<% locals.scriptSection.push('/lib/markitup/markdown.js') %>
<% locals.scriptSection.push('/lib/ajaxfileupload.js') %>
<% include header %>
<div id="preview-wrap" class="preview-wrap">
	<!-- <iframe id="iframePreview" src="/usermanage/preview"></iframe> -->
	<div id="iframePreview"></div>
</div>
<textarea id="markItUp"></textarea>

<div id="image-dialog-form" style="display:none">
	<!-- <input id="thumbnail" type="file" name="thumbnail" /> -->
	<form class="form-horizontal">
      	<fieldset>
			<div class="control-group">
			  <label class="control-label" for="txt-image-title">图片名称：</label>
			  <div class="controls">
			    <input type="text" class="input-xlarge" id="txt-image-title">
			  </div>
			</div>
			<div class="control-group">
			  <label class="control-label" for="txt-image-url">图片地址：</label>
			  <div class="controls">
			    <input type="text" value="http://" class="input-xlarge" id="txt-image-url">
			  </div>
			</div>
      	</fieldset>
    </form>
</div>
<div id="link-dialog-form" style="display:none">
	<form class="form-horizontal">
      	<fieldset>
			<div class="control-group">
			  <label class="control-label" for="txt-link-title">链接标题：</label>
			  <div class="controls">
			    <input type="text" class="input-xlarge" id="txt-link-title">
			  </div>
			</div>
			<div class="control-group">
			  <label class="control-label" for="txt-link-url">链接地址：</label>
			  <div class="controls">
			    <input type="text" value="http://" class="input-xlarge" id="txt-link-url">
			  </div>
			</div>
      	</fieldset>
    </form>
</div>
<% include footer %>

<script type="text/javascript">
var markObj = $('#markItUp').markItUp(mySettings);

// setInterval(function() {
// 	preview();
// 	renderPreview();
// }, 3000);

function resize() {
	var bodyEl = $(document.body),
		w = bodyEl.width(),
		h = bodyEl.height(),
		w2 = (w / 2) - 20,
		h2 = document.documentElement.clientHeight - 40;
	$('#markItUp').width(w2).height(h2 - 5);
	$('#iframePreview').width(w2).height(h2 + 12);
}
$(window).resize(function() {
	resize();
});
$(function() {
	resize();
})

var imageDialog = $("#image-dialog-form").dialog({
	autoOpen: false,
	height: 300,
	width: 350,
	modal: true,
	buttons: {
		"上传图片": function() {
			// var self = $(this);
			// $.ajaxFileUpload({
			// 	url: '/fileupload',
			// 	secureuri: false,
			// 	fileElementId: 'thumbnail',
			// 	dataType: 'json',
			// 	beforeSend: function() {
			// 		// $("#loading").show();
			// 	},
			// 	complete: function() {
			// 		// $("#loading").hide();
			// 	},
			// 	success: function(data, status) {
			// 		self.dialog("close");
			// 	},
			// 	error: function(data, status, e) {
			// 		alert(e);
			// 	}
			// })
			// return false;
			// [id]: url/to/image  "Optional title attribute"
			var title = $('#txt-image-title').val(),
				url = $('#txt-image-url').val(),
				template = '![{title}]({url} "{title}")';
			title = title || url;
			var inputTxt = template.replace(/\{title\}/g, title).replace(/\{url\}/g, url);
			markObj.trigger('insertion', {
				placeHolder: inputTxt
			});
			$('#txt-image-title').val('');
			$('#txt-image-url').val('http://');
			$(imageDialog).dialog("close");
		},
		Cancel: function() {
			$(this).dialog("close");
		}
	},
	close: function() {
		// allFields.val("").removeClass("ui-state-error");
	}
});
var linkDialog = $("#link-dialog-form").dialog({
	autoOpen: false,
	height: 300,
	width: 350,
	modal: true,
	buttons: {
		"确定": function() {
			var title = $('#txt-link-title').val(),
				url = $('#txt-link-url').val(),
				template = '[{title}]({url} "{title}")';
			title = title || url;
			var inputTxt = template.replace(/\{title\}/g, title).replace(/\{url\}/g, url);
			markObj.trigger('insertion', {
				placeHolder: inputTxt
			});
			$('#txt-link-title').val('');
			$('#txt-link-url').val('http://');
			$(linkDialog).dialog("close");
		},
		Cancel: function() {
			$(this).dialog("close");
		}
	},
	close: function() {
		// allFields.val("").removeClass("ui-state-error");
	}
});

function dialogEditImage() {
	imageDialog.dialog('open')
}

function dialogEditLink() {
	linkDialog.dialog('open');
	$('#txt-link-url').focus();
	var el = document.getElementById('txt-link-url'),
		pos = getCursortPosition(el);
	el.focus();
	setCaretPosition(el, 'http://'.length);
}

function setCaretPosition(ctrl, pos) { //设置光标位置函数
	if(ctrl.setSelectionRange) {
		ctrl.focus();
		ctrl.setSelectionRange(pos, pos);
	} else if(ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}

function getCursortPosition(ctrl) { //获取光标位置函数
	var CaretPos = 0; // IE Support
	if(document.selection) {
		ctrl.focus();
		var Sel = document.selection.createRange();
		Sel.moveStart('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if(ctrl.selectionStart || ctrl.selectionStart == '0') {
		CaretPos = ctrl.selectionStart;
	}
	return(CaretPos);
}


var a ='![https://console.appfog.com/images/af-logo.png](https://console.appfog.com/images/af-logo.png "https://console.appfog.com/images/af-logo.png")  test image\n\
\n\
__test test test__\n\
\n\
# [Markdown](http://en.wikipedia.org/wiki/Markdown "Markdown") \n\
[Markdown](http://en.wikipedia.org/wiki/Markdown "Markdown") 是非常轻量级的标记语言，由 [John Gruber](http://en.wikipedia.org/wiki/John_Gruber "John_Gruber") 和 [Aaron Swartz](http://en.wikipedia.org/wiki/Aaron_Swartz "Aaron Swartz") 发明。现在许多人发邮件都喜欢用 HTML 格式了，但是供开发者交流的邮件列表大部分还是纯文本的，Markdown 的语法从纯文本邮件的格式惯例中借鉴了不少经验。“标记语言”的英文就是 Markup language, 从 Markdown 的名字即可看出它的意图，参见 Aaron 最初宣布 Markdown 的[帖子](http://www.aaronsw.com/weblog/001189 "帖子")。\n\
\n\
# [Markdown 语法](http://qingbo.net/picky/502-markdown-syntax.html "Markdown 语法")\n\
## Markdown 相关资源\n\
### Markdown 转 HTML\n\
\n\
Markdown 是比较容易读，但是大部分时候它都需要被转换成 HTML。以下是各种语言对 Markdown 的实现：\n\
\n\
+ Python - Markdown in Python (easy_install Markdown)\n\
+ Ruby\n\
    1. BlueCloth. P.S. 它使用的 project tracking tool 是 python 写的 Trac\n\
    2. kramdown\n\
+ C# - MarkdownSharp\n\
+ JavaScript - Showdown. 本站的评论框实时预览就是在客户端用 Showdown 做的。\n\
+ PHP - PHP Markdown\n\
\
### Markdown 编辑器\n\
\n\
最简单的方式，从身边找一个[支持列编辑模式的文本编辑器](http://qingbo.net/picky/509-column-editing.html "支持列编辑模式的文本编辑器")。\n\
\n\
但是 Markdown 多用在网站上，所以使用一个专门为 Markdown 做的在线编辑器是非常有必要的。[WMD](http://wmd-editor.com/ "WMD") 就是比较好的一个，大家可以看看它的 [demo](http://wmd-editor.com/demo "demo")。\n\
\n\
## 谁在用 Markdown?\n\
\n\
+ [qingbo](http://qingbo.net/ "qingbo") - 本站...使用了 Python Markdown, Showdown\n\
+ [Stack Overflow](http://stackoverflow.com/  "Stack Overflow") - MarkdownSharp 的创造者\n\
+ [GitHub](http://GitHub.com "GitHub")\n\
\n\
## 关于 Aaron Swartz\n\
\n\
我最初知道这位 Aaron Swartz 并不是通过使用 Markdown，而是使用 web.py 框架。没错，web.py 就是他最先开始开发的。去他的 wikipedia 页面上看看吧，14 岁时是 RSS 1.0 specification 的 co-author, 从那儿就开始是 W3C RDF Core Working Group 的成员，在 Stanford University 上了一年后离开去全职运作自己的公司 Infogami. 再看他自己写的 [resume](http://www.aaronsw.com/resume.txt "resume")，Founder of reddit.com, 2006: Time Magazine\'s Person of the Year, 等等一大堆令人羡慕的成绩。别忘了，他出生于 1986 年。\n\
\n\
[Aaron Swartz 的个人网站](http://www.aaronsw.com/ "Aaron Swartz 的个人网站")。目前他的研究兴趣似乎在政治方面，他的文章一直有思想，有内涵，在看到他的详细介绍之前，我就感觉他是和 [Douglas Crockford](http://www.crockford.com/ "Douglas Crockford") 一类的博学多才的中年人……'
markObj.val(a)
preview();
renderPreview();
// console.log(window.markdown.toHTML())
// 2 Answers
// Given a Tuple for eg. (a, b, c)..
// Output : (*, *, *), (*, *, c), (*, b, *), (*, b, c), (a, *, *), (a, *, c), (a, b, *), (a, b, c)
// function p(ary){
// 	var len = ary.length;
// 	for(;len-- >= 0;){
// 	}
// }
// 
// 
/*
ou have a two dimensional array of size m*n. The 
elements in the rows are sorted and every row has 
unique elements means( in a row no two elements are same) but 
the elements can repeat across the rows. 
For example: 
you have following 2-D array: 
{
2 5 7 9 14 16 
3 6 8 10 15 21 
4 7 9 15 22 35 
7 8 9 22 40 58
} 
You are supposed to write an efficient function which 
will take upper 2-D array as input and will return a 
one-dimensional array with following properties: 
a) the 1-D array must contain all the elements of above 
2-D array. 
b) the 1-D array should not have any repeated elements. 
c) all the elements should be in sorted order. 
For example: 
for the above 2-D array, the output should be: 
A [ ] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16, 21, 22, 35, 
40, 58 } 
Function Prototype should be : 
int [ ] MergeAndSort( int[ ][ ] inputArray )


 */
var inputAry = [
	[2, 5, 7, 9, 14, 16],
	[3, 6, 8, 10, 15, 21],
	[4, 7, 9, 15, 22, 35],
	[7, 8, 9, 22, 40, 58]
]

function mergeAndSort(input) {
	var contain = [],
		res, v;
	for(var i in input) {
		contain = contain.concat(input[i]);
	}

	for(var i in input) {
		var ary = input[i];
		for(var j in ary) {
			v = ary[j];
			if(!(v in contain)) {
				contain.push(v)
			}
		}
	}

	var res = contain.sort(function(a, b) {
		return a - b
	})
	return res;
}
// console.log(mergeAndSort(inputAry));
</script>